package cn.zhouchao.org.controller;

import cn.zhouchao.basic.util.AjaxResult;
import cn.zhouchao.basic.util.PageList;
import cn.zhouchao.org.domain.Employee;
import cn.zhouchao.org.query.EmployeeQuery;
import cn.zhouchao.org.service.IEmployeeService;
import cn.zhouchao.system.domain.Role;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    /**
     * 引入Service接口
     */
    @Autowired
    public IEmployeeService iEmployeeService;

    /**
     * 查询所有员工
     * Restful 风格 路径变量
     * http://localhost:8080/emp
     * @return
     */
    @GetMapping()
    public List<Employee> findAll() {
        return iEmployeeService.findAll();
    }

    /**
     * 查询单个员工
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询详情")
    public Employee findById(@ApiParam(value = "部门id", required = true) @PathVariable("id") Long id) {
        return iEmployeeService.findById(id);

    }

    /**
     * 添加或者修改员工
     *
     * @param employee
     * @return
     */
    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        if (null != employee.getId()) {
            // 修改
            iEmployeeService.update(employee);
        } else {
            // 添加
            iEmployeeService.add(employee);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除员工
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        iEmployeeService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 批量删除员工
     *
     * @return
     */
    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        iEmployeeService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     */
    @PostMapping()
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery employeeQuery) {
        PageList<Employee> employeePageList = iEmployeeService.queryByPage(employeeQuery);
        return employeePageList;
    }


}
